package com.bwie.parkdemo.mvc;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.blankj.utilcode.util.SPStaticUtils;
import com.bwie.lib_core.action.ActActionField;
import com.bwie.parkdemo.R;
import com.bwie.parkdemo.dao.LoginUserServe;
import com.bwie.parkdemo.mvvm.model.entity.LoginEntity;
import com.bwie.parkdemo.mvvm.ui.activity.LoginActivity;
import com.jaeger.library.StatusBarUtil;

/**
 * 应用程序启屏页
 * 应用程序启动默认等待1.5s
 * */
public class SplashActivity extends AppCompatActivity {

    private LoginUserServe userServe;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        StatusBarUtil.setTranslucent(this,0);
        userServe = new LoginUserServe(getApplication());
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //判断本地是否缓存用户,无缓存直接登录
                        //执行查询操作->根据数据库中是否具有用户数据来判定用户是否登录
                        LoginEntity.Values values = userServe.selUser();
                        if (values == null){
                            Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                            startActivity(intent);
                        }else {
                            if (values.uId > 0){
                                //已记录用户登录状态，跳转到主页面
                                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putString(ActActionField.ACTBUNDLE, SPStaticUtils.getString("home"));
                                intent.putExtra(ActActionField.ACTBUNDLE,bundle);
                                startActivity(intent);
                            }else {
                                Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                                startActivity(intent);

                            }
                        }
                        finish();
                    }
                });
            }
        }.start();

    }
}
